/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.client.goods.logic;

import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import mscs.data.pojo.Goods;
import mscs.data.pojo.GoodsAnalyse;
import mscs.data.pojo.Operation;

/**
 *
 * @author Usbuild
 */
public class GoodsAnalyseUtil {
    private OperationUtil operationUtil = new InOperationUtil();
    private ISearchGoodsUtil searchGoodsUtil = new GoodsUtil();
    public ArrayList<GoodsAnalyse> searchByCriteria(String keywords, String key) {
        ArrayList<Goods> goodsList = searchGoodsUtil.queryByCriteria(keywords, key);
        ArrayList<GoodsAnalyse> goodsAnalyseList = new ArrayList<GoodsAnalyse>();
        for(Goods goods : goodsList) 
            goodsAnalyseList.add(analyse(goods));
        return goodsAnalyseList;
    }
    public Vector<Vector<String>> toDateTable(String keywords, String key) {
        return toDataTable(searchByCriteria(keywords, key));
    }
    public Vector<Vector<String>> toDataTable(ArrayList<GoodsAnalyse> goodsAnalyseList) {
        try {
            Vector<GoodsAnalyse> seList = new Vector(goodsAnalyseList);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (GoodsAnalyse goodsAnalyse : seList)
            {
                reList.add((Vector<String>)toStringArray(goodsAnalyse));
            }
            return reList;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
    }
    private Vector<String> toStringArray(GoodsAnalyse goodsAnalyse) {
        Vector<String> list = new Vector<String>();
        list.add(goodsAnalyse.getId());
        list.add(goodsAnalyse.getDesc());
        list.add(String.valueOf(goodsAnalyse.getStoreableDay()));
        list.add(String.valueOf(goodsAnalyse.getTotalSal()));
        list.add(String.valueOf(goodsAnalyse.getRulerDay()));
        list.add(String.valueOf(goodsAnalyse.getEveryDaySal()));
        list.add(String.valueOf(goodsAnalyse.getPredictDay()));
        list.add(String.valueOf(goodsAnalyse.getPredictDesPer()));
        return list;
    }
    
    
    private GoodsAnalyse analyse(Goods goods) {
        GoodsAnalyse goodsAnalyse = new GoodsAnalyse();
        ArrayList<Operation> opList = operationUtil.queryByExample(goods);
        Operation operation = opList.get(opList.size() - 1);
        Goods lastGoods = new Goods();
        for(Goods tmpGoods : (ArrayList<Goods>)operation.getGoodsList())
            if(tmpGoods.getId().equals(goods.getId())) {
                lastGoods = tmpGoods;
                break;
        }
        int storeNum = searchGoodsUtil.queryByExample(lastGoods).getNum();
        goodsAnalyse.setId(goods.getId());
        goodsAnalyse.setDesc(goods.getDesc());
        goodsAnalyse.setStoreableDay(subDate(lastGoods.getDesDate(), new Date()));//可存天数
        goodsAnalyse.setTotalSal(lastGoods.getNum() + operation.getGoodsNum().get(lastGoods.getId()) - storeNum);//总流通量
        goodsAnalyse.setRulerDay(subDate(new Date(), operation.getDate()));//尺度天数
        if(goodsAnalyse.getRulerDay() == 0) {
            goodsAnalyse.setEveryDaySal(-1);
            goodsAnalyse.setPredictDay(-1);
            goodsAnalyse.setPredictDesPer(-1);
        } else {
            goodsAnalyse.setEveryDaySal(goodsAnalyse.getTotalSal() / goodsAnalyse.getRulerDay());//每日流通
            if(goodsAnalyse.getEveryDaySal() > 0) {
                goodsAnalyse.setPredictDay(Math.min(storeNum / goodsAnalyse.getEveryDaySal(), goodsAnalyse.getStoreableDay()));
                if(goodsAnalyse.getPredictDay() < goodsAnalyse.getStoreableDay())
                    goodsAnalyse.setPredictDesPer(0);
                else
                    goodsAnalyse.setPredictDesPer(1 - goodsAnalyse.getPredictDay() * 1.0 / goodsAnalyse.getStoreableDay());
            }
            else {
                goodsAnalyse.setPredictDay(-1);
                goodsAnalyse.setPredictDesPer(-1);
            }
        }
        return goodsAnalyse;
    }
    private int subDate(Date newDate, Date oldDate) {
        return (int)(newDate.getTime() - oldDate.getTime()) / (1000*86400);
    }
}
